import keyboard
import pyautogui
import threading
import time

# 定义一个标志来表示是否处于模拟按键状态
is_simulating = False

# 禁用 PyAutoGUI 的 fail-safe 机制
pyautogui.FAILSAFE = False

# 模拟按键的函数
def simulate_key_press():
    while is_simulating:
        try:
            pyautogui.press('4')
            time.sleep(0.1)
        except pyautogui.FailSafeException:  # 捕获 fail-safe 异常
            pass  # 忽略异常

# 监听键盘输入的回调函数
def on_key_event(event):
    global is_simulating
    if event.event_type == "down":
        if keyboard.is_pressed('ctrl+shift+alt+o') and not is_simulating :            
            is_simulating = True
            threading.Thread(target=simulate_key_press).start()  # 在新线程中启动模拟按键
        elif keyboard.is_pressed('ctrl+shift+alt+s'):
            is_simulating = False

# 监听键盘输入
keyboard.hook(on_key_event)

# 保持程序运行
keyboard.wait('esc')
